package com.example.a111;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.VideoView;

import com.github.javafaker.Faker;




import java.util.Locale;
import java.util.Random;
import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;
public class MainActivity extends AppCompatActivity {
    private boolean showDanamaku;
    private DanmakuView danmakuView;
    private DanmakuContext danmakuContext;
    private Button sendButton;
    private LinearLayout sendLayout;
    private EditText editText;
    private VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        playVideo();
        initDanamaku();
    }
    private void initView() {
        videoView = (VideoView) findViewById(R.id.videoview);
        sendLayout = (LinearLayout) findViewById(R.id.ly_send);
        sendButton = (Button) findViewById(R.id.btn_send);
        editText = (EditText) findViewById(R.id.et_text);
        danmakuView = (DanmakuView) findViewById(R.id.danmaku);
    }
    private void playVideo() {
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.sun;
        if (uri != null) {
            videoView.setVideoURI(Uri.parse(uri));
            videoView.start();
        } else {
            videoView.getBackground().setAlpha(0);//将背景设为透明
        }
    }
    private BaseDanmakuParser parser = new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };
    private void initDanamaku() {
        danmakuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                showDanamaku = true;
                danmakuView.start();
                generateDanmakus();
            }
            @Override
            public void updateTimer(DanmakuTimer timer) {
            }
            @Override
            public void danmakuShown(BaseDanmaku danmaku) {
            }
            @Override
            public void drawingFinished() {
            }
        });
        danmakuContext = DanmakuContext.create();
        danmakuView.enableDanmakuDrawingCache(true);//提升屏幕绘制效率
        danmakuView.prepare(parser, danmakuContext);//进行弹幕准备
        //为danmakuView设置点击事件
        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendLayout.getVisibility() == View.GONE){
                    sendLayout.setVisibility(View.VISIBLE);//显示布局
                }else {
                    sendLayout.setVisibility(View.GONE);
                }
            }
        });
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = editText.getText().toString();
                if (!TextUtils.isEmpty(content)){
                    addDanmaku(content,true);
                    editText.setText("");
                }
            }
        });
    }
    private void addDanmaku(String content, boolean border){
        BaseDanmaku danmaku = danmakuContext.mDanmakuFactory.createDanmaku(
                BaseDanmaku.TYPE_SCROLL_RL);
        danmaku.text = content;
        danmaku.padding = 6;
        danmaku.textSize = 30;
        danmaku.textColor = Color.WHITE;//弹幕文字的颜色
        danmaku.setTime(danmakuView.getCurrentTime());
        if (border){
            danmaku.borderColor = Color.BLUE;//弹幕文字边框的颜色
        }
        danmakuView.addDanmaku(danmaku);     //添加一条弹幕
    }
    /**
     136      * 随机生成一些弹幕内容
     137      */

    private void generateDanmakus(){
        final Faker faker = new Faker(new Locale("zh-CN"));
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (showDanamaku) {
                    int num = new Random().nextInt(300);
                    String content = faker.name().name();
                    addDanmaku(content, false);
                    try {
                        Thread.sleep(num);
                    } catch (InterruptedException e) {
                        e.printStackTrace();

                    }

                }


            }

        }).start();
    }
    @Override
    protected void onPause(){
       super.onPause();
       if (danmakuView !=null && danmakuView.isPaused()){
           danmakuView.pause();
       }
    }
    @Override
    protected void onResume(){
        super.onResume();
        if (danmakuView != null && danmakuView.isPrepared() &&
                danmakuView.isPaused()) {
            danmakuView.resume();
        }
        }
    @Override
protected void onDestroy(){
        super.onDestroy();
        showDanamaku = false;
        if (danmakuView !=null){
            danmakuView.release();
            danmakuView = null;
        }
    }
    }
